﻿// -----------------------------------------------------------------------
// <copyright file="BufferOfImage.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
using LastVersionOfIpPackage.Enum_needed;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BufferOfImage
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static BufferOfImage buffer = new BufferOfImage();

        /// <summary>
        /// Gets or sets the buffer.
        /// </summary>
        /// <value>
        /// The buffer.
        /// </value>
        public static BufferOfImage Buffer
        {
            get { return BufferOfImage.buffer; }
            set { BufferOfImage.buffer = value; }
        }

        /// <summary>
        /// Gets the buffer of input image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns>buffer of image</returns>
        public Color[,] GetBuffer(Bitmap image)
        {
            try
            {
                Color[,] bufferOfImage = new Color[image.Width, image.Height];
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        bufferOfImage[j, i] = image.GetPixel(j, i);
                    }
                }

                return bufferOfImage;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Gets the image of input buffer.
        /// </summary>
        /// <param name="bufferOfImage">The buffer of image.</param>
        /// <returns>image of this buffer</returns>
        public Bitmap GetImage(Color[,] bufferOfImage)
        {
            try
            {
                Bitmap image = new Bitmap(bufferOfImage.GetLength(0), bufferOfImage.GetLength(1));
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        image.SetPixel(j, i, bufferOfImage[j, i]);
                    }
                }

                return image;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Gets the image of input buffer.
        /// </summary>
        /// <param name="bufferOfRed">The buffer of red in image.</param>
        /// <param name="bufferOfGreen">The buffer of green in image.</param>
        /// <param name="bufferOfBlue">The buffer of blue in image.</param>
        /// <returns>image of this buffer</returns>
        public Bitmap GetImage(int[,] bufferOfRed, int[,] bufferOfGreen, int[,]bufferOfBlue)
        {
            try
            {
                Bitmap image = new Bitmap(bufferOfRed.GetLength(0), bufferOfGreen.GetLength(1));
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        image.SetPixel(j, i, Color.FromArgb(bufferOfRed[j, i], bufferOfGreen[j, i], bufferOfBlue[j, i]));
                    }
                }

                return image;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Gets the image of buffer in specific color.
        /// </summary>
        /// <param name="color">The color need appear in image.</param>
        /// <param name="bufferOfSpecificColor">buffer of specific color.</param>
        /// <returns>image of input buffer with zero in non needed color</returns>
        public Bitmap GetImage(ColorNeed color, int[,] bufferOfSpecificColor)
        {
            int[] values = new int[3];
            int index = 0;
            switch (color)
            {
                case ColorNeed.Red:
                    {
                        values[1] = 0;
                        values[2] = 0;
                        index = 0;
                    }

                    break;
                case ColorNeed.Green:
                    {
                        values[0] = 0;
                        values[2] = 0;
                        index = 1;
                    }

                    break;
                case ColorNeed.Blue:
                    {
                        values[1] = 0;
                        values[0] = 0;
                        index = 2;
                    }

                    break;
            }

            try
            {
                Bitmap image = new Bitmap(bufferOfSpecificColor.GetLength(0), bufferOfSpecificColor.GetLength(1));
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        values[index] = bufferOfSpecificColor[j, i];
                        image.SetPixel(j, i, Color.FromArgb(values[0], values[1], values[2]));
                    }
                }

                return image;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Gets the buffer of color of input buffers in int.
        /// </summary>
        /// <param name="bufferOfRed">The buffer of red.</param>
        /// <param name="bufferOfGreen">The buffer of green.</param>
        /// <param name="bufferOfBlue">The buffer of blue.</param>
        /// <returns>buffer of colors</returns>
        public Color[,] GetBuffer(int[,] bufferOfRed, int[,] bufferOfGreen, int[,] bufferOfBlue)
        {
            try
            {
                Color[,] buffer = new Color[bufferOfRed.GetLength(0), bufferOfGreen.GetLength(1)];
                for (int i = 0; i < buffer.GetLength(1); i++)
                {
                    for (int j = 0; j < buffer.GetLength(0); j++)
                    {
                        buffer[j, i] = Color.FromArgb(bufferOfRed[j, i], bufferOfGreen[j, i], bufferOfBlue[j, i]);
                    }
                }

                return buffer;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Gets the buffer of color of specific color with buffer of int.
        /// </summary>
        /// <param name="color">The color need .</param>
        /// <param name="bufferOfSpecificColor">the buffer of specific color.</param>
        /// <returns>buffer of color</returns>
        public Color[,] GetBuffer(ColorNeed color, int[,] bufferOfSpecificColor)
        {
            int[] values = new int[3];
            int index = 0;
            switch (color)
            {
                case ColorNeed.Red:
                    {
                        values[1] = 0;
                        values[2] = 0;
                        index = 0;
                    }

                    break;
                case ColorNeed.Green:
                    {
                        values[0] = 0;
                        values[2] = 0;
                        index = 1;
                    }

                    break;
                case ColorNeed.Blue:
                    {
                        values[1] = 0;
                        values[0] = 0;
                        index = 2;
                    }

                    break;
            }

            try
            {
                Color[,] buffer = new Color[bufferOfSpecificColor.GetLength(0), bufferOfSpecificColor.GetLength(1)];
                for (int i = 0; i < buffer.GetLength(1); i++)
                {
                    for (int j = 0; j < buffer.GetLength(0); j++)
                    {
                        values[index] = bufferOfSpecificColor[j, i];
                        buffer[j, i] = Color.FromArgb(values[0], values[1], values[2]);
                    }
                }

                return buffer;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }
    }
}
